package com.netease.nim.demo.main.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.GlideUtil;
import com.netease.nim.demo.common.ui.imageview.HeadImageView;
import com.netease.nimlib.sdk.team.model.Team;
import com.shwread.android.qysw10000243.R;

import java.util.List;

import logic.bean.ContactInfo;
import logic.util.Utils;


public class ContactsAdapter
        extends
        BaseAdapter
{
	
	protected final Context      context;
	
	protected final Fragment     fragment;
	private final LayoutInflater inflater;
	
	private List<Object>         items;
	private ContactInfo item0;
	private int                  pageType; //pagetype 1 为客户经理，2为客户
	private int topNum = 0;
	                                       
	public ContactsAdapter(Context context, List<Object> items) {
		super();
		this.context = context;
		this.fragment = null;
		this.items = items;
		this.inflater = LayoutInflater.from(context);

	}
	
	//	public ContactsAdapter(Context context, List<Team> items, ContactInfo item0, int pageType) {
	//		super();
	//		this.context = context;
	//		this.fragment = null;
	//		this.items = items;
	//		this.item0 = item0;
	//		this.pageType = pageType;
	//		this.inflater = LayoutInflater.from(context);
	//	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return items == null ? 0 : items.size();
	}
	
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position < getCount() ? items.get(position) : null;
	}
	
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = new ViewHolder();
		if (convertView == null)
		{
			convertView = inflater.inflate(R.layout.new_i_contacts_list_item, parent, false);
			holder.civHead = (HeadImageView) convertView.findViewById(R.id.new_i_contacts_list_item_head);
			holder.tvTeamName = (TextView) convertView.findViewById(R.id.new_i_contacts_list_item_name);
			holder.ivMangeIcon = (ImageView) convertView.findViewById(R.id.new_i_contacts_manger_icon);
			holder.rlTeamParent = (RelativeLayout) convertView.findViewById(R.id.rlTeamParent);
			//			holder.tvTeamIntro = (TextView) convertView.findViewById(R.id.new_i_contacts_list_item_intro);
			//			holder.tvTeamCreatTime = (TextView) convertView.findViewById(R.id.new_i_contacts_list_item_creat_time);
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		
		final Object obj = getItem(position);
		if (obj instanceof Team)
		{
			Team team = (Team) obj;
			//			holder.civHead.loadBuddyHeadImage(team.getId());
			holder.civHead.setImageResource(R.drawable.avatar_group);
			holder.tvTeamName.setText(team.getName());
			holder.ivMangeIcon.setVisibility(View.GONE);
			if (position<topNum){
				Log.e("position----topNum",position+"------"+topNum);
				holder.rlTeamParent.setBackgroundColor(Color.parseColor("#f2f2f2"));
			}else{
				holder.rlTeamParent.setBackgroundColor(Color.parseColor("#ffffff"));
			}
		}
		else if (obj instanceof ContactInfo)
		{
			ContactInfo contactInfo = (ContactInfo) obj;
			if (Utils.isEmpty(contactInfo.getNickName()))
			{
				holder.tvTeamName.setText(contactInfo.getAccount());
			}
			else
			{
				holder.tvTeamName.setText(contactInfo.getNickName());
			}
			holder.ivMangeIcon.setVisibility(View.VISIBLE);
			if (Utils.isEmpty(contactInfo.getHeadImage()))
			{
				holder.civHead.setImageResource(R.drawable.avatar_def);
			}
			else
			{
				holder.civHead.loadBuddyHeadImage(contactInfo.getAccId());
				GlideUtil.load(context,contactInfo.getHeadImage(),R.drawable.avatar_def,holder.civHead);
			}
		}
		
		return convertView;
	}
	
	class ViewHolder
	{
		HeadImageView civHead;
		TextView      tvTeamName;
		TextView      tvTeamIntro;
		TextView      tvTeamCreatTime;
		TextView      tvUnreadMsg;
		ImageView     ivMangeIcon;
		RelativeLayout rlTeamParent;
	}

	public void setTopNum(int num){
		this.topNum = num;
	}
}
